package com.zhiwei.gateway.controller;

import com.zhiwei.common.constant.ErrorEnum;
import com.zhiwei.common.constant.ResultResponseMsg;
import com.zhiwei.gateway.dao.entity.UserInfo;
import com.zhiwei.gateway.service.UserTokenService;
import com.zhiwei.gateway.service.WhiteListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * function 网关对外服务端口
 * author zhiwei.yang
 * time 2020-2-28-10:11
 */
@RestController
@RequestMapping("/gateway")
public class GatewayController {

    @Autowired
    private UserTokenService userTokenService;

    @Autowired
    private WhiteListService whiteListService;

    /**
     * Token校验
     * @param token
     * @return
     */
    @GetMapping("/token/check")
    public ResultResponseMsg checkToken(String token){
        if(StringUtils.isEmpty(token)){
            return ResultResponseMsg.buildResultMsg(ErrorEnum.HTTP_TOKEN, null);
        }
        UserInfo userInfo = userTokenService.getUserInfoByToken(token);
        return null != userInfo ? ResultResponseMsg.buildSuccessResultMsg(userInfo.getUsername()) :
                ResultResponseMsg.buildResultMsg(ErrorEnum.HTTP_TOKEN, null);
    }

    /**
     * URL白名单(SecurityFilter无需安全校验)
     * @return
     */
    @GetMapping("/whiteList")
    public ResultResponseMsg whiteList(){
        return ResultResponseMsg.buildSuccessResultMsg(whiteListService.queryValidWhiteList());
    }
}
